package com.top.art.manager;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.top.art.bo.ScoreBO;
import com.top.art.mapper.ExamineeInfoMapper;
import com.top.art.service.IExaminationPaperService;
import com.top.art.vo.ResultList;
import com.top.art.vo.ScoreVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author zhubg
 * @title: ScoreManager
 * @projectName art-exam-manager
 * @description: TODO
 * @date 2020/10/8-10:56
 */
@Component
public class ScoreManager {

    @Autowired
    ExamineeInfoMapper examineeInfoMapper;
    @Autowired
    private IExaminationPaperService examinationPaperService;

    public ResultList<ScoreVO> query(ScoreBO bo) {
        ResultList<ScoreVO> ret = new ResultList<>();
        PageHelper.startPage(bo.getCurrent(), bo.getSize());
        List<ScoreVO> list = examineeInfoMapper.queryScore(bo);
        PageInfo pageInfo = new PageInfo(list);
        ret.setTotal(pageInfo.getTotal());
        ret.setPageNum(bo.getCurrent());
        ret.setPageSize(bo.getSize());
        if (!list.isEmpty()) {
            for (ScoreVO vo : list) {
                List<ScoreVO> examlist = examinationPaperService.queryScoreByCode(vo.getAdmissionTicketCode());
                if (examlist != null && !examlist.isEmpty()) {
                    for (ScoreVO sc : examlist) {
                        vo.setSumiao(sc.getSumiao());
                        vo.setSumiaoRankInProvince(sc.getSumiaoRankInProvince());
                        vo.setSecai(sc.getSecai());
                        vo.setSecaiRankInProvince(sc.getSecaiRankInProvince());
                        vo.setSuxie(sc.getSuxie());
                        vo.setSuxieRankInProvince(sc.getSuxieRankInProvince());
                        vo.setSheji(sc.getSheji());
                        vo.setShejiRankInProvince(sc.getShejiRankInProvince());
                    }
                } else {
                    vo.setSumiao(0);
                    vo.setSecai(0);
                    vo.setSuxie(0);
                    vo.setSheji(0);
                }
            }
        }
        ret.setList(list);
        return ret;
    }

    public List<ScoreVO> downloadScoreByStudio(String studioCode) {
        ScoreBO bo = new ScoreBO();
        bo.setStudioCode(studioCode);
        List<ScoreVO> list = examineeInfoMapper.queryScore(bo);
        if (!list.isEmpty()) {
            for (ScoreVO vo : list) {
                List<ScoreVO> examlist = examinationPaperService.queryScoreByCode(vo.getAdmissionTicketCode());
                if (!examlist.isEmpty()) {
                    for (ScoreVO sc : examlist) {
                        if (sc.getSumiao() != null) {
                            vo.setSumiao(sc.getSumiao());
                        } else {
                            vo.setSumiao(0);
                        }
                        if (sc.getSecai() != null) {
                            vo.setSecai(sc.getSecai());
                        } else {
                            vo.setSecai(0);
                        }
                        if (sc.getSuxie() != null) {
                            vo.setSuxie(sc.getSuxie());
                        } else {
                            vo.setSuxie(0);
                        }
                        if (sc.getSheji() != null) {
                            vo.setSheji(sc.getSheji());
                        } else {
                            vo.setSheji(0);
                        }
                    }
                } else {
                    vo.setSumiao(0);
                    vo.setSecai(0);
                    vo.setSuxie(0);
                    vo.setSheji(0);
                }
            }
        }
        return list;
    }

    public ScoreVO getScoreByTicketCode(String ticketCode) {
        ScoreBO bo = new ScoreBO();
        bo.setTicketCode(ticketCode);
        List<ScoreVO> list = examineeInfoMapper.queryScore(bo);
        if (list != null && !list.isEmpty()) {
            ScoreVO score = list.get(0);
            List<ScoreVO> examlist = examinationPaperService.queryScoreByCode(ticketCode);
            if (!examlist.isEmpty()) {
                for (ScoreVO sc : examlist) {
                    if (sc.getSumiao() != null) {
                        score.setSumiao(sc.getSumiao());
                    } else {
                        score.setSumiao(0);
                    }
                    if (sc.getSecai() != null) {
                        score.setSecai(sc.getSecai());
                    } else {
                        score.setSecai(0);
                    }
                    if (sc.getSuxie() != null) {
                        score.setSuxie(sc.getSuxie());
                    } else {
                        score.setSuxie(0);
                    }
                    if (sc.getSheji() != null) {
                        score.setSheji(sc.getSheji());
                    } else {
                        score.setSheji(0);
                    }
                }
            } else {
                score.setSumiao(0);
                score.setSecai(0);
                score.setSuxie(0);
                score.setSheji(0);
            }
            return score;
        }
        return new ScoreVO();
    }

}
